<script setup>
import { Edit, Delete, User } from "@element-plus/icons-vue";
import { HomeworkUserPageService,
  HomeworkUserAddService,
  HomeworkUserUpdateService,
  HomeworkUserDeleteService
 } from "@/api/homework.js";
import { ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

// 用户搜索时的关键字
const inputHomeworkId = ref("");
const inputName = ref("");

// 用户成绩列表数据模型
const PageList = ref([]);

// 分页条数据模型
const pageNum = ref(1); // 当前页
const total = ref(20); // 总条数
const pageSize = ref(10); // 每页条数

// 当每页条数发生变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size;
  getUserPageList();
};

// 当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num;
  getUserPageList();
};

// 获取用户作业成绩列表数据
const getScorePageList = async () => {
  // 设置查询参数
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    // 有值就传，否则 null
    name: inputName.value ? inputName.value : null,
    HomeworkId: inputHomeworkId.value ? inputHomeworkId.value : null,
  };
  let result = await HomeworkUserPageService(params);
  // 渲染用户列表数据
  PageList.value = result.data.items;
  total.value = result.data.total;
};

getScorePageList();

// 控制抽屉是否显示
const visibleDrawer = ref(false);
//展示抽屉
const showAddDrawer = () => {
  //清空modle
  Model.value = { userId: "", homeworkId: "", state: "", totalScore: "" };
  visibleDrawer.value = true;
};
//展示抽屉
const showUpdetaDrawer = (row) => {
  // 回显数据
  Model.value = row;
  visibleDrawer.value = true;
};

// 添加表单数据模型
const Model = ref({
 userId: "",
  totalScore: "",
 state: "",
 homeworkId:"",
});

// 控制按钮是创建还是修改
const isAdd = ref(true);

// 添加请求
const Add = async () => {
  let result = await   HomeworkUserAddService(Model.value);
  // 弹窗提示
  ElMessage.success(result.msg ? result.msg : "添加成功");
  visibleDrawer.value = false;
  // 刷新列表
  getScorePageList();
};

// 修改请求
const Update = async () => {
  let result = await HomeworkUserUpdateService(Model.value);
  ElMessage.success(result.message ? result.message : "修改成功");
  visibleDrawer.value = false;
getScorePageList();
};


//用户作业删除的请求
const userHKDelete = async (id) => {
  //弹框询问
  let confirm = await ElMessageBox.confirm('此操作将永久删除, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  //确认删除
  if(confirm){
    //调用接口
  let result = await HomeworkUserDeleteService(id);
  // 弹窗提示
  ElMessage.success(result.msg ? result.msg : "删除成功");
  // 刷新列表
  getScorePageList();
  }
};



</script>

<template>
  <el-card class="page-container transparent-card">
    <template #header>
      <div class="header">
        <div class="extra">
          <el-button type="primary" @click="showAddDrawer"
            >添加成绩</el-button
          >
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item>
        <el-input
          v-model="inputHomeworkId"
          style="width: 200px"
          placeholder="请输入作业id"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="inputName"
          style="width: 200px"
          placeholder="请输入姓名"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getScorePageList">搜索</el-button>
        <el-button
          @click="
            inputName = '';
            inputHomeworkId = '';
          "
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="PageList" style="width: 100%">
      <el-table-column label="用户id" prop="userId"></el-table-column>
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="作业id" prop="homeworkId"></el-table-column>
      <el-table-column label="分数" prop="totalScore"></el-table-column>
      <el-table-column label="状态" prop="state">
        <template #default="{ row }">aw大    a
          {{ row.state === 1 ? "批改" : "未批改" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="showUpdetaDrawer(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="userHKDelete(row.id)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 10, 15]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <!-- 抽屉 -->
    <el-drawer
      v-model="visibleDrawer"
      title="添加成绩"
      direction="rtl"
      size="50%"
    >
      <el-form :model="courseModel" label-width="100px">
        <el-form-item label="用户id">
          <el-input
            v-model="Model.userId"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="作业id">
          <el-input
            v-model="Model.homeworkId"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-input
            v-model="Model.state"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="分数">
          <el-input
            v-model="Model.totalScore"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="isAdd ? Add() : Update()"
          >
            {{ isAdd ? "创建" : "更新" }}
          </el-button>
          <el-button type="info" @click="visibleDrawer = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>
</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.transparent-card {
  background-color: rgba(255, 255, 255, 0.8); /* 半透明背景 */
  backdrop-filter: blur(10px); /* 模糊效果 */
  border-radius: 8px; /* 圆角 */
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 可选：轻微阴影效果 */
}

.el-select {
  width: 200px;
}

/* 抽屉样式 */
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}

/* 顶部菜单样式 */
.el-menu-demo {
  margin-bottom: 20px;
}

/* 用户头像样式 */
.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
}
</style>
